package com.zx.dsptuneup_cloud.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.dsptuneup_cloud.Base.BaseAdapter;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.base.tools.tools.StringUtils;
import com.zx.dsptuneup_cloud.R;
import com.zx.dsptuneup_cloud.activity.MatrixVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MatrixAdapter extends BaseAdapter {
    private Context context;
    private List<MatrixVo> gridViewBeanList;

    private OnDbChangeListener onDbChangeListener;
    private SparseBooleanArray mSelectedPositions = new SparseBooleanArray();

    public List<MatrixVo> getGridViewBeanList() {
        return gridViewBeanList;
    }

    public void setOnDbChangeListener(OnDbChangeListener onDbChangeListener) {
        this.onDbChangeListener = onDbChangeListener;
    }

    public void setGridViewBeanList(List<MatrixVo> gridViewBeanList) {
        this.gridViewBeanList = gridViewBeanList;
        mSelectedPositions = new SparseBooleanArray();

        for (int i=0;i<gridViewBeanList.size();i++){
            setItemChecked(i, gridViewBeanList.get(i).getType()==1);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(View.inflate(context, R.layout.item_list_matrix,null));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        MatrixVo childDeviceBean = gridViewBeanList.get(position);
        viewHolder.sel_check.setSelected(isItemChecked(position));

        viewHolder.matrix_db.setText(childDeviceBean.getGain()+"");
      /*  if (viewHolder.sel_check.isSelected()){

            //  viewHolder.matrix_db.clearFocus();;
            viewHolder.matrix_db.setFocusable(false);
            viewHolder.matrix_db.setFocusableInTouchMode(false);


        }else {

            //  viewHolder.matrix_db.requestFocus();
            viewHolder.matrix_db.setFocusable(true);
            viewHolder.matrix_db.setFocusableInTouchMode(true);


        }*/
        viewHolder.matrix_db.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_DONE) {
                    String searchTxt = viewHolder.matrix_db.getText().toString();

                    int type = isItemChecked(position) == true ? 1 : 0;
                    if(onDbChangeListener != null){
                        onDbChangeListener.DbChange(searchTxt,position,type);
                    }
                }
                return false;
            }
        });
        viewHolder.sel_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewHolder.matrix_db.getText().toString().isEmpty()){
                    Toast.makeText(context,"输入的值有误",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (isItemChecked(position)) {
                    setItemChecked(position, false);
                } else {
                    setItemChecked(position, true);
                }
                notifyDataSetChanged();
                int type = isItemChecked(position) == true ? 1 : 0;
                if(onDbChangeListener != null){
                    onDbChangeListener.DbChange(viewHolder.matrix_db.getText().toString(),position,type);
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return gridViewBeanList.size();
    }

    public MatrixAdapter(Context context, List<MatrixVo> gridViewBeanList) {
        super(context);
        this.context = context;
        this.gridViewBeanList = gridViewBeanList;
    }

    @Override
    public void notifyDataSetChanged(List dataList) {

    }

    //设置给定位置条目的选择状态
    private void setItemChecked(int position, boolean isChecked) {
        mSelectedPositions.put(position, isChecked);
    }

    /**
     * 全选
     * */
    public void sellectAll() {
        for (int i = 0;i<gridViewBeanList.size(); i++){
            setItemChecked(i,true);
        }
        notifyDataSetChanged();
    }

    /**
     * 取消全选
     * */
    public void cancelSelectAll() {
        for (int i = 0;i<gridViewBeanList.size(); i++){
            setItemChecked(i,false);
        }
        notifyDataSetChanged();
    }

    //根据位置判断条目是否选中
    private boolean isItemChecked(int position) {
        return mSelectedPositions.get(position);
    }
    //获得选中条目的结果
    public ArrayList<MatrixVo> getSelectedItem() {
        ArrayList<MatrixVo> selectList = new ArrayList<>();
        for (int i = 0; i < gridViewBeanList.size(); i++) {
            if (isItemChecked(i)) {
                selectList.add(gridViewBeanList.get(i));
            }
        }
        return selectList;
    }

    public interface OnDbChangeListener {
        /**
         * 回调方法
         */
        void DbChange(String db,int position,int type);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.matrix_db_edt)
        EditText matrix_db;

        @BindView(R.id.sel_check)
        TextView sel_check;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

    }
}
